import java.util.LinkedList;
import java.util.Queue;

public class SolutionLCP7 {
    public int numWays(int n, int[][] relation, int k) {
        int[][] map=new int[n][n];
        for (int[] ints : relation) {
            map[ints[0]][ints[1]]=1;
        }
        Queue<int[]> queue=new LinkedList<>();
        queue.add(new int[]{0,0});
        int ans=0;
        while (!queue.isEmpty()){
            int[] temp=queue.poll();
            if (temp[1]>=k){
                continue;
            }
            for (int i=0;i<n;i++){
                if (map[temp[0]][i]==1){
                    if (i==n-1 && temp[1]==k-1){
                        ans++;
                    }
                    queue.offer(new int[]{i,temp[1]+1});
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new SolutionLCP7().numWays(5,new int[][]{{0,2},{2,1},{3,4},{2,3},{1,4},{2,0},{0,4}},3));
    }
}
